package sitedao

import (
	"errors"
	"hft/Database/admin/friendly_link"
	"hft/e"

	"gorm.io/gorm"
)

func InitFriendlyLink(db *gorm.DB, city int) (err error) {
	// 找出链接模板
	var gloFriendlyLinkList []*friendly_link.GloFriendlyLink
	db.Find(&gloFriendlyLinkList)
	// 删除原城市模板
	if err = db.Unscoped().Where("city = ?", city).Delete(&friendly_link.CityFriendLyLink{}).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	// 更新模板
	var newCityLink []*friendly_link.CityFriendLyLink
	for _, v := range gloFriendlyLinkList {
		newCityLink = append(newCityLink, &friendly_link.CityFriendLyLink{Title: v.Title, Link: v.Link, City: city, Code: int(v.ID)})
	}
	db.Create(&newCityLink)
	return nil
}
